using System;
using System.Collections.Generic;

namespace IQIGame.Onigao.Game.Editors
{
    public class EdtCfgFSMUtil
    {
        public static void ParseParamValues(string paramText, List<Type> paramTypes, List<object> paramValues)
        {
            string[] paramTxts = paramText.Split(',');
            foreach (var paramTxt in paramTxts)
            {
                ParseParamValue(paramTxt, out var paramType, out var paramValue);
                paramTypes.Add(paramType);
                paramValues.Add(paramValue);
            }
        }

        public static void ParseParamValue(string paramTxt, out Type paramType, out object paramValue)
        {
            string lowParamTxt = paramTxt.ToLower();
            if (lowParamTxt == "true" || lowParamTxt == "false")
            {
                paramType = typeof(bool);
                paramValue = lowParamTxt == "true";
            }
            else if (int.TryParse(paramTxt, out int intParam))
            {
                paramType = typeof(int);
                paramValue = intParam;
            }
            else if (float.TryParse(paramTxt, out float floatParam))
            {
                paramType = typeof(float);
                paramValue = floatParam;
            }
            else
            {
                paramType = typeof(string);
                paramValue = paramTxt;
            }
        }

        public static byte ConvertType(Type type)
        {
            if (type == typeof(bool))
            {
                return (byte)CFSMParamType.Bool;
            }
            else if (type == typeof(int))
            {
                return (byte)CFSMParamType.Int;
            }
            else if (type == typeof(float))
            {
                return (byte)CFSMParamType.Float;
            }
            else if (type == typeof(string))
            {
                return (byte)CFSMParamType.String;
            }
            return 0;
        }

        /// <summary>
        /// 获取状态机配置的短路径名，不包括root和后缀
        /// </summary>
        /// <returns></returns>
        public static string GetCfgFileName(string rootPath, string filePath)
        {
            string relativeFilePath = filePath.Replace(rootPath, string.Empty);
            relativeFilePath = relativeFilePath.Replace("\\", "/");
            if (relativeFilePath.StartsWith('/'))
            {
                relativeFilePath = relativeFilePath.Substring(1); //去掉第一个/
            }
            relativeFilePath = relativeFilePath.Substring(0, relativeFilePath.LastIndexOf('.'));
            return relativeFilePath;
        }
    }
}